package com.housediyadmin.view
{
	import com.housediyadmin.events.buildingEvents.AddBuildingEvent;
	import com.housediyadmin.events.buildingEvents.DeleteBuildingEvent;
	import com.housediyadmin.events.buildingEvents.GetBuildingEvent;
	import com.housediyadmin.events.houseEvents.AddHouseEvent;
	import com.housediyadmin.events.houseEvents.DeleteHouseEvent;
	import com.housediyadmin.events.houseEvents.GetHousesEvent;
	import com.housediyadmin.events.treeEvents.DeleteAreaLevelEvent;
	import com.housediyadmin.events.treeEvents.GetAreaTreeEvent;
	import com.housediyadmin.model.AreaTreeModel;
	import com.housediyadmin.model.BuildingModel;
	import com.housediyadmin.model.HouseModel;
	import com.housediyadmin.service.BuildingRemoteObjectService;
	import com.housediyadmin.view.components.BuildingPopUp;
	import com.housediyadmin.view.components.EditBuildingView;
	import com.housediyadmin.view.components.EditTreeView;
	import com.housediyadmin.view.components.HousePopUp;
	import com.housediyadmin.vo.Building;
	import com.housediyadmin.vo.House;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.DateField;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.events.CalendarLayoutChangeEvent;
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;
	import mx.events.ListEvent;
	import mx.formatters.DateFormatter;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	/**
	 * s
	 * @author wenjun cai
	 * 2010/11/16
	 */	
	public class BuildingAdminMediator extends Mediator
	{
		[Inject]
		public var view:BuildingAdminView;
		
		[Inject]
		public var buildingTreeModel:AreaTreeModel;
		
		[Inject]
		public var buildingModel:BuildingModel;
		
		[Inject]
		public var houseModel:HouseModel;
		
		[Inject]
		public var building:BuildingRemoteObjectService;
		
		override public function onRegister():void
		{
			BindingUtils.bindProperty(view.buildingList, "dataProvider", buildingModel, "buildingArray");		
			BindingUtils.bindProperty(view.houseList, "dataProvider", houseModel, "houseArray");
			
			
			//Tree相关
			view.buildingTree.tree.addEventListener(ListEvent.ITEM_CLICK, treeSelectNode);
			view.buildingTree.deleteBtn.addEventListener(MouseEvent.CLICK, deleteTreeNode);
			
			
			//building相关
			view.addBuildingBtn.addEventListener(MouseEvent.CLICK,addBuildingPopUp);
			view.deleteSeveralBuildingBtn.addEventListener(MouseEvent.CLICK,deleteSeveralBuilding);
			view.buildingList.addEventListener(ListEvent.CHANGE,buildingChangeSelectedItem);
		
			//House相关
			view.addHouseBtn.addEventListener(MouseEvent.CLICK,addHousePopUp);
			view.deleteSeveralHouseBtn.addEventListener(MouseEvent.CLICK,deleteHouse);
			view.houseList.addEventListener(ListEvent.CHANGE,houseChangeSelectedItem);
		}
		
		
		
		
		//House
		private function addHousePopUp(e:MouseEvent):void
		{
			if(buildingModel.selectedBuilding!=null)
			{
				var popup:HousePopUp=new HousePopUp();
				popup.type="ADD";
				mediatorMap.createMediator(popup);
				PopUpManager.addPopUp(popup,contextView,true);
				PopUpManager.centerPopUp(popup);
			}
			else
			{
				Alert.show("请先选择房型所属的楼盘！");
			}
		}
		private function deleteHouse(e:MouseEvent):void
		{
			if(houseModel.selectedHouseArr.length == 0)
			{
				
				Alert.show("请选择删除的户型");
			}
			else
			{
				Alert.yesLabel="是";
				Alert.noLabel="否";
				Alert.cancelLabel="取消";
				Alert.show("删除相应的户型信息吗？","",3,view,deleteHouseAlert);
			}
			
			//dispatch(new DeleteHouseEvent(DeleteHouseEvent.DELETE_HOUSES,houseModel.selectedHouseArr, houseModel.buildingId));
		}
		
		private function deleteHouseAlert(e:CloseEvent):void
		{
			if(e.detail == Alert.YES)
			{
				if(houseModel.selectedHouseArr.length == 0)
				{
					
					Alert.show("请选择删除的户型");
				}
				else
				{
					var houseIds:Array=new Array();
					
					for each(var house:House in houseModel.selectedHouseArr)
					{
						house.isSelected=false;
						houseIds.push(house.houseID);
					}
					dispatch(new DeleteHouseEvent(DeleteHouseEvent.DELETE_HOUSES,houseIds, houseModel.buildingId));	
				}
			}
		}
		private function houseChangeSelectedItem(e:IndexChangeEvent):void
		{
			houseModel.selectedHouse=e.target.selectedItem as House;						
		}
		
		
		//Building
		private var buildingIds:ArrayCollection=new ArrayCollection();
		private var typeIds:Array=new Array();
		private function deleteSeveralBuilding(e:MouseEvent):void
		{
			buildingIds.removeAll();
			typeIds.length=0;
			
			if(buildingModel.selectedBuildingArr.length==0)
			{
				Alert.show("请先选择批量删除对象！");
				return;
			}
			for each(var building:Building in buildingModel.selectedBuildingArr)
			{
				buildingIds.addItem(building.buildingID);
				typeIds.push(building.areaID);
			}
			dispatch(new GetHousesEvent(GetHousesEvent.GET_HOUSE_IDS,buildingIds));
			
			Alert.yesLabel="是";
			Alert.noLabel="否";
			Alert.cancelLabel="取消";
			Alert.show("确定删除？","",3,view,deleteBuildingAlert);
		}
		
	
		private function deleteBuildingAlert(e:CloseEvent):void
		{
			if(e.detail==Alert.YES)
			{
				if(houseModel.houseArray && houseModel.houseArray.length!=0)
				{
					Alert.show("删除失败！还有未删除的房型！");
					return;
				}
				dispatch(new DeleteBuildingEvent(DeleteBuildingEvent.DELETE_BUILDING,buildingIds.source,typeIds));
			}
		}
		
		private function buildingChangeSelectedItem(e:ListEvent):void
		{
			buildingModel.selectedBuilding = e.target.selectedItem as Building;											
		}
		
		private function addBuildingPopUp(e:MouseEvent):void
		{
			if(buildingTreeModel.selectedNode!=null)
			{
				var popup:EditBuildingView=new EditBuildingView();
				popup.type="ADD";
				mediatorMap.createMediator(popup);
				PopUpManager.addPopUp(popup,contextView,true);
				PopUpManager.centerPopUp(popup);
			}
			else
			{
				Alert.show("请先选择楼盘所属的区域！");
			}
		}
		
		//tree
		private function deleteTreeNode(e:MouseEvent):void
		{
			Alert.yesLabel="是";
			Alert.noLabel="否";
			Alert.cancelLabel="取消";
			Alert.show("确定删除？","",3,view,deleteTreeNodeAlert);
		}
		
		
		private function deleteTreeNodeAlert(e:CloseEvent):void
		{
			if(e.detail==Alert.YES)
			{
				var selectedTreeNode:XML=buildingTreeModel.selectedNode;
				var treeToDelID:int=selectedTreeNode.@typeID;
				
				if(selectedTreeNode.children().length()!=0)
				{
					Alert.show("还有下级结点未删除。删除失败！");
					return;
				}
				
				//结点下有building的，不给删 
				for each(var building:Building in buildingModel.buildingArray)
				{
					if(building.areaID==treeToDelID)
					{
						Alert.show("树节点下还有楼盘未删除。删除失败！");
						return;
					}
				}
				dispatch(new DeleteAreaLevelEvent(DeleteAreaLevelEvent.DELETE_AREA_LEVEL,treeToDelID));
			}
		}
			
			
		private function treeSelectNode(event:ListEvent):void
		{
			var selectedItem:XML=XML(event.target.selectedItem);
			buildingTreeModel.selectedNode=selectedItem;
			
			var areaIDs:ArrayCollection = new ArrayCollection();

			//获取selectedItem下的所有孩子及孙子的@typeID
			
			var xmlList:XMLList = selectedItem..@typeID;
			//待查找的areaID: xmlList[0],xmlList[1],xmlList[2].......
			if(xmlList.length()!=0)
			{
				for each(var xml:XML in xmlList)
				{
					areaIDs.addItem((int)(xml.toXMLString()));
				}
				dispatch(new GetBuildingEvent(GetBuildingEvent.GET_BUILDING_IDS,areaIDs, null, null));	
			}
		}
	}
}